package swing;

import misc.Files;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import static javax.swing.JFileChooser.DIRECTORY_CHANGED_PROPERTY;
import static javax.swing.JFileChooser.SELECTED_FILE_CHANGED_PROPERTY;
import static misc.Files.Extension;

public class Images {
    public final static Extension JPEG = new Extension("jpeg");
    public final static Extension JPG = new Extension("jpg");
    public final static Extension GIF = new Extension("gif");
    public final static Extension TIFF = new Extension("tiff");
    public final static Extension TIF = new Extension("tif");
    public final static Extension PNG = new Extension("png");

    public static final Extension[] EXTENSIONS = new Extension[]{
            JPEG, JPG, GIF, TIFF, TIF, PNG
    };

    public static Image LoadImage(String path) {
        return new ImageIcon(path).getImage();
    }

    //Obtain the image URL
    public static Image loadImage(Class<?> aClass, String path) {
        try {
            return ImageIO.read(aClass.getResource(path));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /* demos.swing.imageChooser.Filter.java is used by FileChooserDemo2.java. */
    public static class Filter extends FileFilter {

        //Accept all directories and all GIF, JPG, TIFF, or PNG files.
        public boolean accept(File file) {
            if (file.isDirectory())
                return true;

            Extension extension = Files.getExtension(file);

            return extension != null && Arrays.asList(EXTENSIONS).contains(extension);
        }

        //The description of this filter
        public String getDescription() {
            return "Just Images";
        }
    }

    /* Preview.java by FileChooserDemo2.java. */
    public static class Preview extends JComponent
            implements PropertyChangeListener {
        /**
         *
         */
        private static final long serialVersionUID = -7453156840106347061L;
        ImageIcon thumbnail = null;
        File file = null;

        public Preview(JFileChooser fc) {
            setPreferredSize(new Dimension(100, 50));
            fc.addPropertyChangeListener(this);
        }

        public void loadImage() {
            if (file == null) {
                thumbnail = null;
                return;
            }

            //Don't use createImageIcon (which is a wrapper for getResource)
            //because the image we're trying to loadAll is probably not one
            //of this program's own resources.
            ImageIcon tmpIcon = new ImageIcon(file.getPath());
            if (tmpIcon.getIconWidth() > 90) {
                thumbnail = new ImageIcon(tmpIcon.getImage().
                        getScaledInstance(90, -1,
                                Image.SCALE_DEFAULT));
            } else { //no need to miniaturize
                thumbnail = tmpIcon;
            }
        }

        public void propertyChange(PropertyChangeEvent e) {
            boolean update = false;
            String prop = e.getPropertyName();

            //If the directory changed, don't show an image.
            if (DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
                file = null;
                update = true;

                //If a file became selected, find out which one.
            } else if (SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
                file = (File) e.getNewValue();
                update = true;
            }

            //Update the preview accordingly.
            if (update) {
                thumbnail = null;
                if (isShowing()) {
                    loadImage();
                    repaint();
                }
            }
        }

        protected void paintComponent(Graphics g) {
            if (thumbnail == null) {
                loadImage();
            }
            if (thumbnail != null) {
                int x = getWidth() / 2 - thumbnail.getIconWidth() / 2;
                int y = getHeight() / 2 - thumbnail.getIconHeight() / 2;

                if (y < 0) {
                    y = 0;
                }

                if (x < 5) {
                    x = 5;
                }
                thumbnail.paintIcon(this, g, x, y);
            }
        }
    }

}
